<?php
/**
 * Created by IntelliJ IDEA.
 * User: Ilya
 * Date: 23.03.2009
 * Time: 21:56:14
 */

require_once DCES_BASE_DIR . 'utils/ResultsUpdater.php';
require_once DCES_BASE_DIR . 'utils/Problem.php';
require_once DCES_BASE_DIR . 'utils/ResultsTable.php';

/**
 * @param  $contest_id int id of contest
 * @return ContestDescription description of contest
 */
function requestContestSettings($contest_id) {
    $prfx = DB_PREFIX;

    if ($contest_id === 0) throwBusinessLogicError(14);
        $contest_row = Data::getRow(sprintf(
            "SELECT settings
             FROM ${prfx}contest
             WHERE id=%s
            ", Data::quote_smart($contest_id))
        );
    if (!$contest_row) throwBusinessLogicError(14);

    $serialized_contest_settings = $contest_row['settings'];

    return Data::_unserialize($serialized_contest_settings);
}

function processGetContestResultsRequest(GetContestResultsRequest $request) {

    //get $is_anonymous, $contest_id, $user_contest_row, $user_contest_start_time, $contest_settings
    if (!is_null($request->sessionID)) {
        $is_anonymous = false;
        $user_contest_row = RequestUtils::testSession($request->sessionID);

        $user_type = $user_contest_row['user_type'];
        $user_id = $user_contest_row['id'];

        $contest_id = RequestUtils::getRequestedContest(
            $request->contestID,
            $user_contest_row['contest_id'],
            $user_type
        );
        if ($contest_id < 0) throwBusinessLogicError(14);

        if ($user_type === 'SuperAdmin')
            $contest_settings = requestContestSettings($contest_id);
        else {
            $serialized_contest_settings = $user_contest_row['settings'];
            $contest_settings = Data::_unserialize($serialized_contest_settings);
        }

        $user_contest_start_time = DateMySQLToPHP($user_contest_row['contest_start']);
        $user_contest_finish_time = DateMySQLToPHP($user_contest_row['contest_finish']);
    }
    else
    {
        $is_anonymous = true;
        $user_type = null;
        $user_id = -1;
        $contest_id = $request->contestID;
        $user_contest_start_time = null; //contest was not started for anonymous
        $user_contest_finish_time = null; //and was not finished
        $contest_settings = requestContestSettings($contest_id);
    }

    //get $is_admin
    $is_admin = !$is_anonymous && (
            ($user_type === 'SuperAdmin') || ($user_type === 'ContestAdmin')
    );

    //get $permission
    $c_time = getCurrentContestTime($contest_settings, $user_contest_start_time, $user_contest_finish_time);
    if (!$is_admin) {
        if ($c_time['interval'] === 'before') throwBusinessLogicError(19);

        if ($c_time['interval'] === 'contest' && !$c_time['is_ending'])
            $permission = $contest_settings->resultsAccessPolicy->contestPermission;
        else if ($c_time['is_ending'])
            $permission = $contest_settings->resultsAccessPolicy->contestEndingPermission;
        else if ($c_time['interval'] === 'after' && !$c_time['is_ending'])
            $permission = $contest_settings->resultsAccessPolicy->afterContestPermission;
        else
            $permission = "NoAccess"; //should not occur
    }
    else
        $permission = 'FullAccess';

    //test rights
    if ($permission === 'NoAccess') throwBusinessLogicError(0);
    if ($is_anonymous && $permission === "OnlySelfResults") throwBusinessLogicError(0);

    //now all permissions are tested, we may update results table and return result

    //update table
    $updater = new ResultsUpdater($contest_id, $contest_settings->problemsDefaultSettings);
    $updater->refreshResults(false);

    $table = new ResultsTable(
        $contest_id,
        $contest_settings->data,
        $permission === 'OnlySelfResults' ? $user_id : -1,
        $is_admin
    );

    return $table->getTable();
}

?>